package com.jjy.admin.task;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jjy.admin.domain.JobEntity;
import com.jjy.admin.domain.JobSpiderDTO;
import com.jjy.admin.domain.TaskJob;
import com.jjy.admin.mapper.JobMapper;
import com.jjy.admin.mapper.TaskJobMapper;
import com.jjy.admin.service.job.JobService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Component
public class MyTask {
    private static final Logger log = LoggerFactory.getLogger(  MyTask.class);

    @Resource
    private TaskJobMapper taskJobMapper;
    @Autowired
    private JobService jobService;
    @Autowired
    private JobMapper jobMapper;

//    @Scheduled(cron = "0 34 22 * * ?") // 16:00
    @Scheduled(cron = "0 0/30 * * * ?") // 每半小时处理一次

    public void doAynSpider() {
//        查询是否有异步爬取出任务
        TaskJob taskJob = new TaskJob();
        taskJob.setTaskType(1);
        taskJob.setStatus(0);
        List<TaskJob> taskJobs = taskJobMapper.selectTaskList(taskJob);
            if (taskJobs.size() > 0) {
                for (TaskJob job : taskJobs) {
        try {
                    String taskCondition = job.getTaskCondition();
                    JobSpiderDTO jobSpiderCondition=JSONUtil.toBean(taskCondition,JobSpiderDTO.class);
                    int i = jobService.doSpider(jobSpiderCondition);
                    if (i>0){
//                        爬取成功修改状态
                        boolean b = taskJobMapper.updateTaskSuccessStatusById(job.getId());
                        log.info(  "爬取任务成功id："+job.getId()+"任务状态修改："+b);
                       log.info(  "爬取数量："+i);
                    }else {
                        boolean b  = taskJobMapper.updateTaskFailStatusById(job.getId());
                        log.info(  "爬取任务失败id："+job.getId()+"任务状态修改："+b);
                    }
                }catch (Exception e){
            boolean b  = taskJobMapper.updateTaskFailStatusById(job.getId());
            log.info(  "爬取任务失败id："+job.getId()+";任务状态修改："+b);
        }
            }
        }else {
                log.info(  "没有异步爬取任务");
            }
    }

   /* @Scheduled(cron = "0 0 23 * * ?") // 每天23点清洗当天数据
    public void cleanJobData() {
//        拿到当天的数据
        List<JobEntity> jobList = jobMapper.selectList(new LambdaQueryWrapper<JobEntity>().
                ge(JobEntity::getCreateTime, new Date()));
//       遍历数据,判断主要字段是否符合自定标准
        if (!CollectionUtils.isEmpty(jobList)) {
            for (JobEntity job : jobList) {
                Long id = job.getId();
                String jobName = job.getJobName();
                String province = job.getProvince();
                String city = job.getCity();
                String degree = job.getDegree();
                if (job.getJobName() == null || job.getJobName().equals("")) {
                    job.setJobName("未知");
                }

            }
        }

    }*/
}
